<template>
	<div>
		<el-card class="!border-none" shadow="never">
			<el-alert type="warning" title="请确保等级ID按顺序排列" :closable="false" show-icon />
			<el-alert type="warning" title="升级条件: 累积质押金额、1代推荐人数" :closable="false" show-icon />
		</el-card>
		<el-card class="!border-none" v-loading="pager.loading" shadow="never">
			<el-button v-perms="['member.user_member/add']" type="primary" @click="handleAdd">
				<template #icon>
					<icon name="el-icon-Plus" />
				</template>
				新增
			</el-button>
			<div class="mt-4">
				<el-table :data="pager.lists">
					<el-table-column label="等级ID" prop="id" show-overflow-tooltip />
					<el-table-column label="等级名称" prop="name" show-overflow-tooltip />
					<el-table-column label="LOGO" min-width="60">
						<template #default="{ row }">
							<image-contain v-if="row.logo" :src="row.logo" :width="60" :height="45"
								:preview-src-list="[row.logo]" preview-teleported fit="contain" />
						</template>
					</el-table-column>
					<el-table-column label="背景图" min-width="100">
						<template #default="{ row }">
							<image-contain v-if="row.bg_img" :src="row.bg_img" :width="60" :height="45"
								:preview-src-list="[row.bg_img]" preview-teleported fit="contain" />
						</template>
					</el-table-column>
					<el-table-column label="累积质押金额" prop="money" show-overflow-tooltip />
					<el-table-column label="1代推荐人数" prop="level1_num" show-overflow-tooltip />
					<!-- <el-table-column label="推荐人等级" prop="vip_name" show-overflow-tooltip /> -->
					<el-table-column label="每日质押次数" prop="item_num" show-overflow-tooltip />
					<el-table-column label="质押加息率" prop="item_add_rate" show-overflow-tooltip />
					<el-table-column label="挖矿速度" prop="mine_speed" show-overflow-tooltip />
					<!-- <el-table-column label="量化金额" show-tooltip-when-overflow>
						<template #default="{ row }">
							<div>{{row.lh_min}} - {{row.lh_max}}</div>
						</template>
					</el-table-column>
					<el-table-column label="收益率" show-tooltip-when-overflow>
						<template #default="{ row }">
							<div>{{row.rate_min}}% - {{row.rate_max}}%</div>
						</template>
					</el-table-column>
					<el-table-column label="每日量化次数" prop="lh_num" show-overflow-tooltip /> -->
					<el-table-column label="显示状态" prop="is_show">
						<template #default="{ row }">
							<dict-value :options="dictData.show_status" :value="row.is_show" />
						</template>
					</el-table-column>
					<el-table-column label="操作" width="120" fixed="right">
						<template #default="{ row }">
							<el-button v-perms="['member.user_member/edit']" type="primary" link
								@click="handleEdit(row)">
								编辑
							</el-button>
							<el-button v-if="row.id!=1" v-perms="['member.user_member/delete']" type="danger" link
								@click="handleDelete(row.id)">
								删除
							</el-button>
						</template>
					</el-table-column>
				</el-table>
			</div>
			<div class="flex mt-4 justify-end">
				<pagination v-model="pager" @change="getLists" />
			</div>
		</el-card>
		<edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
	</div>
</template>

<script lang="ts" setup name="userMemberLists">
	import { usePaging } from '@/hooks/usePaging'
	import { useDictData } from '@/hooks/useDictOptions'
	import { apiUserMemberLists, apiUserMemberDelete } from '@/api/user_member'
	import { timeFormat } from '@/utils/util'
	import feedback from '@/utils/feedback'
	import EditPopup from './edit.vue'

	const editRef = shallowRef<InstanceType<typeof EditPopup>>()
	// 是否显示编辑框
	const showEdit = ref(false)


	// 查询条件
	const queryParams = reactive({

	})

	// 选中数据
	const selectData = ref<any[]>([])

	// 表格选择后回调事件
	const handleSelectionChange = (val : any[]) => {
		selectData.value = val.map(({ id }) => id)
	}

	// 获取字典数据
	const { dictData } = useDictData('show_status')

	// 分页相关
	const { pager, getLists, resetParams, resetPage } = usePaging({
		fetchFun: apiUserMemberLists,
		params: queryParams
	})

	// 添加
	const handleAdd = async () => {
		showEdit.value = true
		await nextTick()
		editRef.value?.open('add')
	}

	// 编辑
	const handleEdit = async (data : any) => {
		showEdit.value = true
		await nextTick()
		editRef.value?.open('edit')
		editRef.value?.setFormData(data)
	}

	// 删除
	const handleDelete = async (id : number | any[]) => {
		await feedback.confirm('确定要删除？')
		await apiUserMemberDelete({ id })
		getLists()
	}

	getLists()
</script>